/*
 * Created on Jan 7, 2004
 */
package com.unash.server.z3950Server;

/**
 * @author Ashwini Gokhale
 */

import com.unash.z3950.pdu.Request;
import com.unash.z3950.pdu.Response;
import com.unash.z3950.pdu.Z3950BerDecoder;
import com.unash.z3950.pdu.Z3950BerEncoder;
import com.unash.z3950.pdu.Z3950Decoder;
import com.unash.z3950.pdu.Z3950Encoder;


public class Z3950Operation implements Runnable {
	private Thread runner = null;
	Z3950Connection zConn;
	Request objRequest;
	
	public Z3950Operation(Z3950Connection zConn,Request objRequest){
		this.zConn = zConn;
		this.objRequest = objRequest;
	}//Z3950Operation
	
	public void start(){
		if(runner == null){
			runner = new Thread(this,"");
			runner.start();
		}//if
	}//start
	
	
	public void run(){
		Z3950Decoder decoder = new Z3950BerDecoder();
		Z3950Encoder encoder = new Z3950BerEncoder();
		
		try{
			Response objResponse = objRequest.process();
			byte[] result = objResponse.accept(encoder);
			zConn.updateAssociation();
			zConn.write(result);
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}//run
	

}//Z3950Operation
